package com.link.base.base.invloc.controller;


import com.link.base.base.invloc.model.InvImp;
import com.link.base.base.invloc.service.InvImpService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.model.CallParam;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: unknown
 * created: unknown
 * comments:
 */
@Controller
@RequestMapping("/link/invImp")
public class InvImpController extends BasicController<InvImp> {

    @Resource
    private InvImpService invImpService;

    @Override
    public BasicService<InvImp> getBasicService() throws Exception {
        return invImpService;
    }

    @Override
    public void beforQueryAllOrExample(InvImp entity, HttpServletRequest request) throws Exception {
        entity.setStatusTypeLov("IMP_NOT_SUCCESS");
    }

    /**
     * 查询导入成功历史数据
     */
    @RequestMapping(value = "/querySuccessListPage")
    @ResponseBody
    public Map<String, Object> querySucListPage(@JsonParam QueryParams qps, HttpSession session,
                                                HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            qps.preDealSecurity(request);
            //qps.addFilter(new Filter("statusTypeLov", "IMP_SUCCESS"));
            InvImp entity = (InvImp) BasicModel.transformClass(InvImp.class, qps);
            entity.setStatusTypeLov("IMP_SUCCESS");
            List<InvImp> list = invImpService.queryByExamplePage(entity);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(entity, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<InvImp>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> check(@JsonParam CallParam callParam, HttpSession session,
                                     HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessageKey("check")
                .withMessage("校验销量导入...").flush();
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            invImpService.runInvImpCheck(callParam);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/invTxnInsert", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> invTxnInsert(@JsonParam CallParam callParam, HttpSession session,
                                            HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessageKey("invTxnInsert")
                .withMessage("提交数据...").flush();
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            invImpService.runNewInvTxnForimp(callParam);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
